<?php namespace App\Http\Controllers\Api\Distribution;

use App\AvailableUrl;
use App\DistributionWithdraw;
use App\Http\Controllers\Controller;
use App\Http\Services\DomainManager;
use App\Orders;
use App\User;
use EasyWeChat\Factory;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    /**
     * 退款接口（订单不可为空）
     * @param Orders $thisOrder
     * @param $reason
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public static function Refund(DistributionWithdraw $distributionWithdraw){
        $thatUser = User::whereId($distributionWithdraw->user_id)->first();
        if(!$thatUser){
            return false;
        }

        $appid = env("WXAPP_KEY");
        $mchid = env("WECHAT_PAYMENT_MERCHANT_ID");
        $config = DomainManager::getUnPayConfigByAppId($appid,$mchid);
        $app = Factory::payment($config);
        $result = $app->transfer->toBalance([
            'partner_trade_no' => $distributionWithdraw->withdrawal_num, // 商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
            'openid' => $thatUser->wx_openid,
            'check_name' => 'FORCE_CHECK', // NO_CHECK：不校验真实姓名, FORCE_CHECK：强校验真实姓名
            're_user_name' => $distributionWithdraw->name, // 如果 check_name 设置为FORCE_CHECK，则必填用户真实姓名
            'amount' => $distributionWithdraw->withdraws_amount * 100, // 企业付款金额，单位为分
            'desc' => '提现', // 企业付款操作说明信息。必填
        ]);

        return $result;
    }

}
